﻿Imports System.Windows.Forms
Imports Encryptor.My

Public Class dlgCipherText
    Private Const EncFileExt As String = ".aesx"
    Private EcipherText As Byte()
    Private RFileName As String = "C:\" & Transact.Probability.RndStrGen("abcdefghijklmnopqrstuvwxyz", "0123456789", 10) & ".aes.edit"

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        'dimension the input
        Dim plainTextBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(txtIn.Text)
        Dim password As String = MySettings.Default.password

        'run the password and the plain text through the AES encryption system
        Dim encAES As New SimpleAes(password)
        EcipherText = encAES.EncryptData(plainTextBytes)
        sfdEncFile.InitialDirectory = Computer.FileSystem.SpecialDirectories.MyDocuments
        'choose output file
        sfdEncFile.ShowDialog()
    End Sub

    Private Sub sfdEncFile_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles sfdEncFile.Disposed
        Me.Focus()
        txtIn.Focus()
    End Sub

    Private Sub sfdEncFile_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles sfdEncFile.FileOk
        'output the encrypted text to a file
        Me.Close()
        Dim safeName$ = sfdEncFile.FileName.Substring(sfdEncFile.FileName.LastIndexOf("\") + 1)
        Computer.FileSystem.WriteAllBytes(sfdEncFile.FileName, EcipherText, False)
        Computer.FileSystem.RenameFile(sfdEncFile.FileName, safeName & EncFileExt)
        MsgBox("File saved successfully.")
        txtIn.Text = Nothing
        frmCrypto.Show()
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        txtIn.Text = Nothing
        Me.Close()
        frmCrypto.Focus()
    End Sub

    Private Sub dlgCipherText_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        txtIn.Text = Nothing
        txtIn.ReadOnly = True
        txtIn.BackColor = SystemColors.Control
        btnLock.Text = "Lock"
        btnLock.Enabled = True
    End Sub

    Private Sub dlgCipherText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        txtIn.Focus()
    End Sub

    Private Sub dlgCipherText_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtIn.Text = Nothing
        Me.Focus()
        txtIn.Focus()
    End Sub

    Private Sub btnNew_Click(sender As System.Object, e As System.EventArgs) Handles btnNew.Click
        txtIn.Text = Nothing
        txtIn.ReadOnly = False
        txtIn.BackColor = Color.White
        btnLock.Enabled = True
        txtIn.Focus()
    End Sub

    Private Sub btnOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnOpen.Click
        ofdEncFile.ShowDialog()
    End Sub

    Private Sub ofdEncFile_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles ofdEncFile.FileOk
        Select Case logDecrypt.ShowDialog()
            Case Windows.Forms.DialogResult.OK 'If the user clicked OK and the password is good, exit the select block and run the decryption sub
                Exit Select
            Case Windows.Forms.DialogResult.Abort 'If the user clicked OK and the password is bad, exit the whole process with an error message
                MsgBox("Incorrect password.", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation)
                Exit Sub
            Case Windows.Forms.DialogResult.Cancel
                Exit Sub
        End Select

        My.Computer.FileSystem.CopyFile(ofdEncFile.FileName, RFileName)
        System.IO.File.SetAttributes(RFileName, IO.FileAttributes.Hidden)
        Dim decAes As New SimpleAes(MySettings.Default.password)
        Dim plainTextBytes As Byte() = decAes.DecryptData(My.Computer.FileSystem.ReadAllBytes(RFileName))
        txtIn.Text = System.Text.Encoding.UTF8.GetChars(plainTextBytes)
        My.Computer.FileSystem.DeleteFile(RFileName)

        btnLock.Enabled = True
        btnLock.PerformClick()
    End Sub

    Private Sub btnLock_Click(sender As System.Object, e As System.EventArgs) Handles btnLock.Click
        If btnLock.Text = "Lock" Then
            txtIn.ReadOnly = True
            txtIn.BackColor = SystemColors.Control
            btnLock.Text = "Unlock"
        Else
            txtIn.ReadOnly = False
            txtIn.BackColor = Color.White
            btnLock.Text = "Lock"
        End If
    End Sub
End Class
